package top.jojoace.demo.Configuration;

import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import top.jojoace.demo.Dao.ConditionUser;
import top.jojoace.demo.Dao.UserInterface;
import top.jojoace.demo.condition.UserCondition;

/**
 * <h1>条件配置</h1>
 * @author 陈Ser
 * @ClassName ConditionalConfiguration
 * @description: TODO
 * @Date 2021/10/29 14:54
 * @Version 1.0
 */
@Configuration
public class ConditionalConfiguration {


    @Bean
    /**
     * 条件注解，判断该类是否加入容器
     */
    @Conditional(UserCondition.class)
    public UserInterface conditionUser(){
        ConditionUser conditionUser = new ConditionUser();
        conditionUser.sayClass();

        System.out.println("ConditionalConfig...");

        return conditionUser;
    }

    @Bean
    /**
     * <h1>@ConditionalOnXXXXXXX</h1>
     * <h1>    成品_条件注解    </h1>
     */
    /* 配置条件注解 */
    @ConditionalOnProperty(value = "Conditional.condition",havingValue = "ConditionUser")
    public UserInterface condition2(){
        ConditionUser conditionUser = new ConditionUser();
        conditionUser.sayClass();
        return conditionUser;
    }

    @Bean
    // matchIfMissing 参数。默认false。为true时，当配置中没有找到KEY则默认加载。
    @ConditionalOnProperty(value = "Conditional.condition",havingValue = "ConditionUser",matchIfMissing = true)
    public UserInterface condition3(){
        ConditionUser conditionUser = new ConditionUser();
        conditionUser.sayClass();
        return conditionUser;
    }


    /**
     *     @ConditionalOnBean   当SpringIoc容器中存在指定的Bean
     *     @ConditionalOnClass  当SpringIoc容器中存在指定的Class
     *     @ConditionalOnExpression 基于SpEL表达式作为判断条件
     *
     *     @ConditionalOnJava   基于JVM版本作为判断条件
     *     @ConditionalOnJndi   JNDI存在时查找指定的位置
     *     @ConditionalOnProperty   配置属性是否有指定的值
     *     @ConditionalOnResource   类路径是否有指定的值
     *     @ConditionalOnSingleCandidate
     *     当指定Bean在SpringIoc容器内只有一个，或者虽然有多个但是存在指定首选的Bean
     *
     *     @ConditionalOnWebApplication 当前项目是Web项目
     *     @ConditionalOnNotWebApplication  当前项目不是Web项目
     *
     *     @ConditionalOnMissingBean    当SpringIoc容器中不存在指定的Bean
     *     @ConditionalOnMissingClass   当SpringIoc容器中不存在指定的Class
     *     @ConditionalOnMissingFilterBean  当SpringIoc容器中不存在指定的FilterBean
     */

}
